package pATT.profilingAlgorithm;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.Vector;

import pATT.core.InstanceManager;
import pATT.core.Instances;
import pATT.core.Chooser.ImageFilter;
import pATT.profile.Profile;
import pATT.profile.ProfileComponent;
import pATT.profilingAlgorithm.PersonalSearcher.HTMLDocument;
import pATT.profilingAlgorithm.PersonalSearcher.Reasoner;
import pATT.profilingAlgorithm.PersonalSearcher.Settings;


public class PersonalSearcherAlgorithm extends BatchAlgorithm {
	
	public double similarityThreshold = Settings.getSimilarityThreshold();
	public double evaluationThreshold = Settings.getEvaluationThreshold();
	public double selectionThreshold = Settings.getSelectionThreshold();
	public double classificationThreshold = Settings.getClassificationThreshold();
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
//	Tree tree = null;
	@Override
	public Profile run(Instances input) {
		
//			String file = Message.PATH_PERSONAL_FILE;
		//levanta prueba1
		if(InstanceManager.eINSTANCE.getFileLoaded() == null){
			return null;
		}
		String file = InstanceManager.eINSTANCE.getFileLoaded().getAbsolutePath();	
		
		if(!file.endsWith("."+ImageFilter.PERSONAL_SEARCHER_EXTENSION)){
			return null;
		}
			Settings.setActiveUser("PATT");
			
			BufferedReader in2 = null;
			try {
				in2 = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			String file2;
			try {
				file2 = in2.readLine();
				while (file2!=null){
					String text = unLoadPage(file2);
					HTMLDocument page1=new HTMLDocument(text,file2);
					double r = between (10,10);
					
					Reasoner.addInstance(page1,file2, r , (new Date()).getTime());
					file2=in2.readLine();
				}
				
			} catch (IOException e) {
				return null;
			}
			Vector<ProfileComponent> vector = new Vector<ProfileComponent>();
			vector.addElement(Reasoner.getProfile());
			Profile profile = new Profile(vector,null);
			
			return profile;
	}
	
	public double between (double minVal, double maxVal) {
		Random generator = new Random(1);
		maxVal = maxVal - 0.1;
		double d = (minVal + maxVal*generator.nextDouble());
		if (d>1) d=1;
		return d;
	}
	
	
	
	private String unLoadPage(String file) throws IOException{
		
		StringBuffer stringbuffer = new StringBuffer();
		try{
			DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
			String s = null;
			while((s = datainputstream.readLine()) != null){
				s=s+" ";
				stringbuffer.append(s);
			}
		}catch(Exception _ex){}
		return stringbuffer.toString();
	}
	
	
	@Override
	public String toText() {
		// TODO Auto-generated method stub
		
		return Reasoner.getProfile().showTree();
	}
	
	@Override
	public String basicAlgorithmOutput() {
		// TODO Auto-generated method stub
		return "nada";
	}
	
	@Override
	public boolean needArffFile() {
		return false;
	}
	
	public double getClassificationThreshold() {
		return classificationThreshold;
	}
	
	public void setClassificationThreshold(double classificationThreshold) {
		Settings.setClassificationThreshold(classificationThreshold);
		this.classificationThreshold = classificationThreshold;
	}
	
	public double getEvaluationThreshold() {
		return evaluationThreshold;
	}
	
	public void setEvaluationThreshold(double evaluationThreshold) {
		Settings.setEvaluationThreshold(evaluationThreshold);
		this.evaluationThreshold = evaluationThreshold;
	}
	
	public double getSelectionThreshold() {
		return selectionThreshold;
	}
	
	public void setSelectionThreshold(double selectionThreshold) {
		Settings.setSelectionThreshold(selectionThreshold);
		this.selectionThreshold = selectionThreshold;
	}
	
	public double getSimilarityThreshold() {
		return similarityThreshold;
	}
	
	public void setSimilarityThreshold(double similarityThreshold) {
		Settings.setSimilarityThreshold(similarityThreshold);
		this.similarityThreshold = similarityThreshold;
	}
	
}
